﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
using System.IO;
using WhiteBoard;

namespace WFFS
{
    public partial class FrmMenu : Form
    {
        FrmUnit frmUnit = null;   //声明一个全局变量
        FrmResouce frmResouce = null;
        FrmSendMSG frmSendMsg = null;
        FrmSound frmSound = null;
        FrmTJ frmTj = null;
        //public FrmMsg frmMsg1 = null;
        FrmGoods frmGoods = null;
        Form1 frmMain = null;
        FrmZC frmZC = null;
        FrmNegotiation frmNegotiation;
        private FrmAlldirective frmAlldirective = null;
        System.Windows.Forms.Timer m_TimerCallerType;

        [DllImport("kernel32")]
        private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        public FrmMenu()
        {
            InitializeComponent();
        }
        public FrmMenu(Form1 _frmMain)
        {
            InitializeComponent();
            frmMain = _frmMain;
        }

        //单位选择
        private void btDwxz_Click(object sender, EventArgs e)
        {
            if (frmUnit == null)
            {
                frmUnit = new FrmUnit(frmMain);
            }
            else if (frmUnit.Text == "")
            {
                frmUnit.Dispose();
                frmUnit = null;
                frmUnit = new FrmUnit(frmMain);
            }
            frmUnit.Show();
            string x = ReadIniData("FrmUnit", "X", "X", Application.StartupPath + @"\WFFS.ini");
            string y = ReadIniData("FrmUnit", "Y", "Y", Application.StartupPath + @"\WFFS.ini");
            frmUnit.Location = new Point(int.Parse(x), int.Parse(y));
            //frmUnit.Location = new Point(200, 200);
            frmUnit.TopMost = true;
        }

        //力量部署
        private void btXcll_Click(object sender, EventArgs e)
        {
            if (frmResouce == null)
            {
                frmResouce = new FrmResouce(frmMain);
            }
            else if (frmResouce.Text == "")
            {
                frmResouce.Dispose();
                frmResouce = null;
                frmResouce = new FrmResouce(frmMain);
            }
            frmResouce.Show();
            string x = ReadIniData("FrmResouce", "X", "X", Application.StartupPath + @"\WFFS.ini");
            string y = ReadIniData("FrmResouce", "Y", "Y", Application.StartupPath + @"\WFFS.ini");
            frmResouce.Location = new Point(int.Parse(x), int.Parse(y));
            //frmResouce.Location = new Point(100, 150);
            frmResouce.TopMost = true;            
        }

        //下达指令
        private void btXdml_Click(object sender, EventArgs e)
        {
            btnJSXX_Click(sender, e);
            if (frmSendMsg == null)
            {
                frmSendMsg = new FrmSendMSG(frmMain, frmMain.frmMsg);
            }
            else if (frmSendMsg.Text == "")
            {
                frmSendMsg.Dispose();
                frmSendMsg = null;
                frmSendMsg = new FrmSendMSG(frmMain, frmMain.frmMsg);
            }
            frmSendMsg.Show();
            string x = ReadIniData("FrmSendMSG", "X", "X", Application.StartupPath + @"\WFFS.ini");
            string y = ReadIniData("FrmSendMSG", "Y", "Y", Application.StartupPath + @"\WFFS.ini");
            frmSendMsg.Location = new Point(int.Parse(x), int.Parse(y));
            //frmSendMsg.Location = new Point(400, 150);
            frmSendMsg.TopMost = true;
            
        }

        //语音通话
        private void btYyfq_Click(object sender, EventArgs e)
        {
            if (frmSound == null)
            {
                frmSound = new FrmSound(frmMain);
            }
            else if (frmSound.Text == "")
            {
                frmSound.Dispose();
                frmSound = null;
                frmSound = new FrmSound(frmMain);
            }
            frmSound.Show();
            string x = ReadIniData("FrmSound", "X", "X", Application.StartupPath + @"\WFFS.ini");
            string y = ReadIniData("FrmSound", "Y", "Y", Application.StartupPath + @"\WFFS.ini");
            frmSound.Location = new Point(int.Parse(x), int.Parse(y));
            //frmSound.Location = new Point(500, 150);
            frmSound.TopMost = true;
            
        }

        //物资消耗
        private void btClxx_Click(object sender, EventArgs e)
        {
            if (frmGoods == null)
            {
                frmGoods = new FrmGoods(frmMain);
            }
            else if (frmGoods.Text == "")
            {
                frmGoods.Dispose();
                frmGoods = null;
                frmGoods = new FrmGoods(frmMain);
            }
            frmGoods.Show();
            string x = ReadIniData("FrmGoods", "X", "X", Application.StartupPath + @"\WFFS.ini");
            string y = ReadIniData("FrmGoods", "Y", "Y", Application.StartupPath + @"\WFFS.ini");
            frmGoods.Location = new Point(int.Parse(x), int.Parse(y));
            frmGoods.TopMost = true;
            btClxx.BackgroundImage = imageList1.Images[2];
            changeBtn("btClxx", "0");
            this.timerCar.Start();
        }


        //及时消息
        private void btnJSXX_Click(object sender, EventArgs e)
        {
            if (frmMain.frmMsg == null)
            {
                frmMain.frmMsg = new FrmMsg();
            }
            else if (frmMain.frmMsg.Text == "")
            {
                frmMain.frmMsg.Dispose();
                frmMain.frmMsg = null;
                frmMain.frmMsg = new FrmMsg();
            }
            frmMain.frmMsg.Show();
            string x = ReadIniData("FrmMsg", "X", "X", Application.StartupPath + @"\WFFS.ini");
            string y = ReadIniData("FrmMsg", "Y", "Y", Application.StartupPath + @"\WFFS.ini");
            frmMain.frmMsg.Location = new Point(int.Parse(x), int.Parse(y));
            //frmMain.frmMsg.Location = new Point(800, 350);
            frmMain.frmMsg.TopMost = true;
            changeBtn("btnJSXX", "0");
        }
        //清场统计
        private void btQctj_Click(object sender, EventArgs e)
        {
            if (frmTj == null)
            {
                frmTj = new FrmTJ(frmMain);
            }
            else if (frmTj.Text == "")
            {
                frmTj.Dispose();
                frmTj = null;
                frmTj = new FrmTJ(frmMain);
            }
            frmTj.Show();
            string x = ReadIniData("FrmTJ", "X", "X", Application.StartupPath + @"\WFFS.ini");
            string y = ReadIniData("FrmTJ", "Y", "Y", Application.StartupPath + @"\WFFS.ini");
            frmTj.Location = new Point(int.Parse(x), int.Parse(y));
            //frmTj.Location = new Point(500, 400);
            frmTj.TopMost = true;
            
        }

        //开始
        private void btStart_Click(object sender, EventArgs e)
        {
            //发个消息给各客户端，开始行动
            //frmMain.StartorEnd("1");
            MessageBox.Show("对不起，请刷卡继续！");
        }

        //结束行动
        private void btnStop_Click(object sender, EventArgs e)
        {
            //frmMain.StartorEnd("0");
            MessageBox.Show("对不起，请刷卡继续！");
        }


        private void FrmMenu_FormClosed(object sender, FormClosedEventArgs e)
        {
            
        }
        private void FrmDisposed(Form frm)
        {
           
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMenu_Load(object sender, EventArgs e)
        {
            //btnJSXX_Click(sender, e);
            this.timerCar.Interval = 5000;
            this.timerCar.Start();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="btnflag"></param>
        /// <param name="flag"></param>
        public void changeBtn(string btnflag,string flag)
        {
            if (btnflag == "btnJSXX")
            {
                if (flag == "1")
                {
                    btnJSXX.BackgroundImage = imageList1.Images[1];
                }
                else
                    btnJSXX.BackgroundImage = imageList1.Images[0];
            }
            if (btnflag == "btClxx")
            {
                if (flag == "1")
                {
                    btClxx.BackgroundImage = imageList1.Images[3];
                }
                else
                    btClxx.BackgroundImage = imageList1.Images[2];
            }
            if (btnflag == "btZC")
            {
                if (flag == "1")
                {
                    btZC.BackgroundImage = imageList1.Images[7];
                }
                else
                    btZC.BackgroundImage = imageList1.Images[6];
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timerCar_Tick(object sender, EventArgs e)
        {
            string sql = "select a.*,b.FullOil,b.FullFoap,b.UnderLineOil,b.UnderLineFoap  " +
               " from InOutInfo a left join CarInfo b on a.RFID=b.RFId "+
                   "where (a.OilSurplus <b.UnderLineOil or a.FoamSurplus <b.UnderLineFoap) ";
            //b.FullOil -  b.FullFoap - 
             SqlDataReader reader1 = null;
            SqlCommand cmd1 = new SqlCommand(sql, frmMain.dbConnect);
            reader1 = cmd1.ExecuteReader();

            while (reader1.Read())
            {
                changeBtn("btClxx", "1");
                reader1.Close();
                this.timerCar.Stop();
                return;
            }
            changeBtn("btClxx", "0");
            reader1.Close();
            this.timerCar.Stop();
            //SqlCommand cmd = new SqlCommand(sql, frmMain.dbConnect);                        
            //SqlDataAdapter datapter011 = new SqlDataAdapter(cmd);
            //DataSet ds011 = new DataSet();
            //datapter011.Fill(ds011);

            //if (ds011.Tables[0].Rows.Count > 0)
            //    changeBtn("btClxx", "1");
            //else
            //    changeBtn("btClxx", "0");
            
            //ds011.Clear();
            //ds011 = null;
            //System.GC.Collect();
            //datapter011.Dispose();

        }

        private void btZC_Click(object sender, EventArgs e)
        {
            if (frmZC== null)
            {
                frmZC = new FrmZC(frmMain);
            }
            else if (frmZC.Text == "")
            {
                frmZC.Dispose();
                frmZC = null;
                frmZC = new FrmZC(frmMain);
            }
            frmZC.Show();
            string x = ReadIniData("FrmZC", "X", "X", Application.StartupPath + @"\WFFS.ini");
            string y = ReadIniData("FrmZC", "Y", "Y", Application.StartupPath + @"\WFFS.ini");
            frmZC.Location = new Point(int.Parse(x), int.Parse(y));
            //frmZC.Location = new Point(100, 100);
            frmZC.TopMost = true;
            changeBtn("btZC", "0");
        }


        /// <summary>
        /// 语音通话按钮开始闪动
        /// </summary>
        public void SoundFlickerStart()
        {
            this.Invoke(new MethodInvoker(delegate()
            {
                if (m_TimerCallerType != null)
                {
                    m_TimerCallerType.Dispose();
                    m_TimerCallerType = null;
                }
                m_TimerCallerType = new System.Windows.Forms.Timer();
                m_TimerCallerType.Tick += new EventHandler(OnTimerCallerTypeChangeImage);
                m_TimerCallerType.Interval = 100;
                m_TimerCallerType.Start();
            }));
        }

        private void OnTimerCallerTypeChangeImage(Object obj, EventArgs e)
        {
            this.Invoke(new MethodInvoker(delegate()
            {
                if (this.m_TimerCallerType.Interval % 2 == 0)
                {
                    btYyfq.BackgroundImage = imageList1.Images[4];
                }
                else
                {
                    btYyfq.BackgroundImage = imageList1.Images[5];
                }
                if (m_TimerCallerType.Interval == 150)
                {
                    m_TimerCallerType.Interval = 100;
                }
                m_TimerCallerType.Interval++;
            }));
        }

        /// <summary>
        /// 语音通话按钮停止闪动,恢复绿色
        /// </summary>
        public void SoundFlickerStop()
        {
            this.Invoke(new MethodInvoker(delegate()
            {
                if (m_TimerCallerType != null)
                {
                    m_TimerCallerType.Dispose();
                    m_TimerCallerType = null;
                }
                btYyfq.BackgroundImage = imageList1.Images[5];
            }));
        }

        /// <summary>
        /// 读取ini文件
        /// </summary>
        /// <param name="Section">段落</param>
        /// <param name="Key">关键字</param>
        /// <param name="NoText">关键字对应的值</param>
        /// <param name="iniFilePath">文件路径</param>
        /// <returns></returns>
        public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                StringBuilder temp = new StringBuilder(1024);
                GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);
                return temp.ToString();
            }
            else
            {
                return String.Empty;
            }
        }
        private void btClose_Click(object sender, EventArgs e){
            Application.Exit();
        }
        private void btNegotiation_Click(object sender, EventArgs e)
        {
            if (frmNegotiation == null)
            {
                frmNegotiation = new FrmNegotiation(frmMain);
            }
            else if (frmNegotiation.Text == "")
            {
                frmNegotiation.Dispose();
                frmNegotiation = null;
                frmNegotiation = new FrmNegotiation(frmMain);
            }
            frmNegotiation.Show();
            string x = ReadIniData("FrmNegotiation", "X", "X", Application.StartupPath + @"\WFFS.ini");
            string y = ReadIniData("FrmNegotiation", "Y", "Y", Application.StartupPath + @"\WFFS.ini");
            frmNegotiation.Location = new Point(int.Parse(x), int.Parse(y));
            frmNegotiation.TopMost = true;
        }

        private void btnAll_Click(object sender, EventArgs e)
        {
            if (frmAlldirective == null)
            {
                frmAlldirective = new FrmAlldirective(frmMain);
            }
            else if (frmAlldirective.Text == "")
            {
                frmAlldirective.Dispose();
                frmAlldirective = null;
                frmAlldirective = new FrmAlldirective(frmMain);
            }
            frmAlldirective.Show();
            string x = ReadIniData("FrmAlldirective", "X", "X", Application.StartupPath + @"\WFFS.ini");
            string y = ReadIniData("FrmAlldirective", "Y", "Y", Application.StartupPath + @"\WFFS.ini");
            frmAlldirective.Location = new Point(int.Parse(x), int.Parse(y));
            frmAlldirective.TopMost = true;
        }

    } 
}
